<?php
if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
    exit('Access Denied');
}

// 获取表模型
$table_configs = C::t('#erling_kami_buy#er_kami_buy_config');

// 处理表单提交
if(submitcheck('configsubmit')) {
    $configs = $_POST['config'];
    foreach($configs as $key => $value) {
        if($key == 'nav_name') {
            $value = str_replace('<script>', '', $value);
        }
        $result = $table_configs->update_by_key($key, array('value' => $value));
        if ($result['success'] === false) {
            $errors[] = "配置项 {$key} 更新失败：{$result['message']}";
        }
    }
    if(empty($errors)) {
        cpmsg('配置更新成功', 'action=plugins&operation=config&do='.$pluginid.'&identifier=erling_kami_buy&pmod=admin_config', 'succeed');
    } else {
        cpmsg(implode('<br>', $errors), 'action=plugins&operation=config&do='.$pluginid.'&identifier=erling_kami_buy&pmod=admin_config', 'error');
    }
}
    
// 获取当前配置

// 获取 get的type参数
$type = $_GET['type']??'site';
$configs = $table_configs->fetch_by_type($type);

// 获取用户组
$all_groups = C::t('common_usergroup')->range();
$system_groups = [];// 系统用户组
$member_groups = [];// 会员用户组
$special_groups = [];// 自定义用户组  
$admin_groups = [];// 自定义管理组    
foreach($all_groups as $group) {
    if($group['type'] == 'special') {
        if($group['radminid'] > 0) {
            $admin_groups[] = $group;
        } else {
            $special_groups[] = $group;
        }
    }
    if($group['type'] == 'system') {
        $system_groups[] = $group;
    }
    if($group['type'] == 'member') {
        $member_groups[] = $group;
    }
}


// 积分类型
$extcredits_array = DB::fetch_all("SELECT * FROM %t WHERE skey LIKE %s", array('common_setting', 'extcredits%'));
if ($extcredits_array && isset($extcredits_array[0]['svalue'])) {
    $extcredits_data = unserialize($extcredits_array[0]['svalue']);
} else {
    $extcredits_data = [];
}
$html_selectapi_extcredits = '<select class="api-extcredits">';
foreach($extcredits_data as $key => $value) {
    $html_selectapi_extcredits .= '<option value="'.$key.'">'.$value['title'].'</option>';
}
$html_selectapi_extcredits .= '</select>';

include template('erling_kami_buy:admin_config');